<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <button id="btn">向后端发送信息</button>
    </div>
    <script>
      const ws = new WebSocket("ws://localhost:8080");
      const idBtn = document.getElementById("btn");

      ws.onopen = () => {
        console.log("Connected success");
      };
      ws.onmessage = (event) => {
        console.log(event.data);
      };
      ws.close = () => {
        console.log("Disconnected");
      };

      idBtn.addEventListener("click", () => {
        ws.send("Hello this is client");
      })
    </script>
  </body>
</html>
